﻿//demo
define(['jquery', 'swfupload'], function () {

    var initSwfUpload = function (restype) {
        var d = new Date();
        var year = d.getFullYear().toString();
        var mon = (d.getMonth() + 1).toString();
        var day = d.getDate().toString();
        var hour = d.getHours().toString();
        var time = day + hour + year + mon;

        var fileUploadSettings = {
            //upload_url: 'http://iupload.gyyx.cn/upload/upload.do?sourceFile=MGPSource&copyFile=MGPThumbnail&ResType=' + restype, //'http://localhost:2003/upload/upload.do' ,
            upload_url: window.config.url.uploadUrl + '?sourceFile=MGPSource&copyFile=MGPThumbnail&ResType=' + restype + '&identify=' + time + "" + restype,
            file_types: '*.jpeg;*.jpg;*.png;*.gif;*.bmp',
            file_types_description: '图片类型',
            file_upload_limit: 0,
            prevent_swf_caching: false,

            button_image_url: '/Content/images/swfupload_btn.png',
            button_cursor: SWFUpload.CURSOR.HAND,
            button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
            button_placeholder_id: 'placeholder',
            button_width: 62,
            button_height: 21,

            file_dialog_complete_handler: fileDialogComplete,
            upload_success_handler: uploadSuccess,
            upload_complete_handler: uploadComplete,
            //upload_error_handler: uploadError,

            flash_url: '/Content/Scripts/Components/SwfUpload/swfupload.swf',
            debug: false
        };
        var fileUpload = new SWFUpload(fileUploadSettings);
    }

    //文件选择框关闭时触发
    var fileDialogComplete = function (selected, queued) {
        if (selected > 1) {
            art.dialog({
                content: '最多只能上传1张图片',
                ok: function () { this.close(); }
            });
            return;
        }
        this.startUpload();
    }

    //文件上传成功后触发
    var uploadSuccess = function (file, serverData) {
        var result = jQuery.parseJSON(serverData);
        if (result.success) {
            $("#videoPicFileKey").val(result.content.FileName);
        }
        else {
            if (result.msg = 'exceed size') {
                art.dialog({
                    content: '单张图片大小不能超过200KB',
                    ok: function () { this.close(); }
                });
            } else {
                art.dialog({
                    content: '上传图片出错',
                    ok: function () { this.close(); }
                });
            }
        }
    }

    //文件上传完成后触发
    var uploadComplete = function (file) {
        //开始上传下一个，如果有的话
        //this.startUpload();
    }

    var uploadError = function (obj, code, msg) {
        alert("swf error code: " + code + "\nhttp status: " + msg + "\n详情咨询开发人员");
    }

    return {
        initSwfUpload: initSwfUpload,
        fileDialogComplete:fileDialogComplete,
        uploadSuccess: uploadSuccess,
        uploadComplete:uploadComplete
    }
});